//
//  MyAFN.m
//  coba-iOS
//
//  Created by 郭四虎 on 15/11/19.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "MyAFN.h"

@implementation MyAFN

//+(AFHTTPSessionManager *) JSONManager
//{
//    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
//    NSDictionary *headDic = appdelegate.appKeyChainDic;
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
//    manager.requestSerializer.timeoutInterval = 30.f;
//    for (NSString *key in headDic.allKeys) {
//        NSString *value = headDic[key];
//        [manager.requestSerializer setValue:value forHTTPHeaderField:key];
//    }
//    return manager;
//}


/*
 *AFget
 */
//+ (void)GetJSONHttp:(NSString *)url Parameters:(NSDictionary *)parameters  Success:(void(^)(id responseObject))SuccessBlock  andFalse:(void(^)( NSError *error))FalseBlock
//{
//    AFHTTPSessionManager *manager =[MyAFN JSONManager];
//    
//    [manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        SuccessBlock(responseObject);
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        FalseBlock(error);
//        
//    }];
//}

+(void)GET:(NSString *)url success:(void (^)(NSDictionary *responseObj))finishBlock error:(void (^)(NSError *error))errorBlock fromController:(UIViewController *)con
{
    NSURLSessionDataTask *operation = nil;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    operation = [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        
        finishBlock(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        errorBlock(error);
    }];
}

+ (NSURLSessionDataTask *)POST:(NSString *)url parameters:(id)parameters block:(void (^)(id responseObject))finishedBlock error:(void(^)(NSError *error))errorBlock fromController:(UIViewController *)con
{
    if (con) {
        [con.view Loading_0314];
    }
    
    NSURLSessionDataTask *operation = nil;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer.timeoutInterval = 10.f;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
//    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    
    operation = [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (con) {
            [con.view Hidden];
        }
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        finishedBlock(dic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        errorBlock(error);
        
        NSLog(@"%@",error);
        
        if (con) {
            [con.view Message:@"请检查网络" HiddenAfterDelay:1];
        }
        
        NSLog(@"ERROR！！！");
    }];
    return operation;
}

+ (NSURLSessionDataTask *)POST:(NSString *)url parameters:(id)parameters withData:(NSData *)data andDataname:(NSString *)name block:(void (^)(id responseObject))finishedBlock error:(void (^)(NSError *))errorBlock fromController:(UIViewController *)con
{
    [con.view Loading_0314];
    
    NSURLSessionDataTask *operation = nil;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    operation = [manager POST:HEADPHOTO parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //        //设置需要上传的文件
        //        //UIImagePNGRepresentation(_iconView.image)-》将图片转成NSData
        //        //第一个参数是需要上传的文件，第二个是后台规定的参数名
        //        //第三个文件名，但是后台一般不用，所以意义不大
        //        //第四个文件类型
        
        [formData appendPartWithFileData:data name:name fileName:@"image.png" mimeType:@"image/png"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [con.view Hidden];
        
        NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        finishedBlock(response);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        errorBlock(error);
        
        NSLog(@"%@",error);
        
        [con.view Message:@"请检查网络" HiddenAfterDelay:1];
        
        NSLog(@"ERROR！！！");
    }];
    
    return operation;
}

//+ (AFHTTPRequestOperation *)POST:(NSString *)url parameters:(id)parameters withData:(NSData *)data andDataname:(NSString *)name block:(void (^)(id))finishedBlock
//{
//    
//    AFHTTPRequestOperation *operation = nil;
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//
//    operation = [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//        
//        [formData appendPartWithFileData:data name:name fileName:@"1.png" mimeType:@"image/png"];
//        
//    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//        NSLog(@"%@",dic);
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"ERROR！！！");
//    }];
//    
//    return operation;
//}


@end
